package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.request.human.StudentsDateOnlineRequest;
import com.ocom.common.request.human.StudentsDetailRequest;
import com.ocom.common.request.human.StudentsKqBydaysDetailRequest;
import com.ocom.common.request.human.StudentsOnlineRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.HumanClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" ) //
public class ChartController {

    @Autowired
    HumanClientService humanClientService;

    @Autowired
    CompanyClientService companyClientService;
    @PostMapping("/studentOnlineShow")
    public Result studentOnlineShow(@RequestBody StudentsOnlineRequest request){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if(yoCiUser.getRole() == 23l && !yoCiUser.getDeptId().equals("0") ){
            Result listResult = humanClientService.getDeptList(yoCiUser.getComId(),Long.valueOf(yoCiUser.getDeptId()),0);
            if(listResult.getCode()==200){
                List<EntityDeptInfo> deptInfoList = JSON.parseArray(JSON.toJSONString(listResult.getData()),EntityDeptInfo.class);
                request.setDeptIds(deptInfoList.stream().map(EntityDeptInfo -> EntityDeptInfo.getDeptId()).collect(Collectors.toList()));
            }
        }

        Map<String,Object> objectMap=new HashMap<>();
        request.setComId(yoCiUser.getComId());
        objectMap.put("list",humanClientService.getKqAndStudentsOnline(request).getData());

        objectMap.put("stateList",new ArrayList<>());

        try {
            Result<List<EntityCompanyConfig>> mjSMS = companyClientService.getCompanyConfig(yoCiUser.getComId(), "regionOnline");
            if (mjSMS.getCode() == 200) {
                String str = mjSMS.getData().get(0).getConfig().replaceAll("\\\\","");
                objectMap.put("stateList", JSON.parseArray(str));
            }
        }catch (Exception e){

        }
        return ResultUtil.success(objectMap);
    }


    @PostMapping("/studentOnlineDetail")
    public Result studentOnlineDetail(@RequestBody StudentsDetailRequest request){

        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();


        if(request.getDeptId()!=null ){
            request.getDeptIds().add(request.getDeptId());
        }
        request.setComId(yoCiUser.getComId());

        return humanClientService.getKqAndStudentsDetail(request);
    }



    @PostMapping("/getStudentDateKq")
    public Result studentOnlineDetail(@RequestBody StudentsDateOnlineRequest request){

        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

        if(yoCiUser.getRole() == 23l ){
            Result listResult = humanClientService.getDeptList(yoCiUser.getComId(),Long.valueOf(yoCiUser.getDeptId()),0);
            if(listResult.getCode()==200){
                List<EntityDeptInfo> deptInfoList = JSON.parseArray(JSON.toJSONString(listResult.getData()),EntityDeptInfo.class);
                request.setDeptIds(deptInfoList.stream().map(EntityDeptInfo -> EntityDeptInfo.getDeptId()).collect(Collectors.toList()));
            }
            if(request.getDeptId()!=null && request.getDeptIds().contains(request.getDeptId())){
                List<Long> deptIds = new ArrayList<>();
                deptIds.add(request.getDeptId());
                request.setDeptIds(deptIds);
            }
        }else{
            if(request.getDeptId()!=null ){
                request.getDeptIds().add(request.getDeptId());
            }
        }

        request.setComId(yoCiUser.getComId());

        return humanClientService.getStudentDateKq(request);
    }


    @PostMapping("/getStudentDateKqDetail")
    public Result getStudentDateKqDetail(@RequestBody StudentsKqBydaysDetailRequest request){

        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

        if(yoCiUser.getRole() == 23l ){
            Result listResult = humanClientService.getDeptList(yoCiUser.getComId(),Long.valueOf(yoCiUser.getDeptId()),0);
            if(listResult.getCode()==200){
                List<EntityDeptInfo> deptInfoList = JSON.parseArray(JSON.toJSONString(listResult.getData()),EntityDeptInfo.class);
                request.setDeptIds(deptInfoList.stream().map(EntityDeptInfo -> EntityDeptInfo.getDeptId()).collect(Collectors.toList()));
            }
            if(request.getDeptId()!=null && request.getDeptIds().contains(request.getDeptId())){
                List<Long> deptIds = new ArrayList<>();
                deptIds.add(request.getDeptId());
                request.setDeptIds(deptIds);
            }
        }else{
            if(request.getDeptId()!=null){
                request.setDeptId(request.getDeptId());
            }
        }
        request.setComId(yoCiUser.getComId());

        return humanClientService.studentsKqBydaysDetail(request);
    }
}
